package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayout.Tab;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.android.material.tabs.TabLayoutMediator.TabConfigurationStrategy;
import easier.recycler.DataViewHolder;
import easier.recycler.SingleTypeAdapter;
import java.util.ArrayList;
import java.util.List;

public class MainActivity35 extends AppCompatActivity {

    private SwipeRefreshLayout mRefresh;
    private ConstraintLayout mContainer;
    private TabLayout mTabLayout;
    private ViewPager2 mViewPager;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity35.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main35 );
        initView();
        initViewAction();
    }

    private void initView() {

        mRefresh = (SwipeRefreshLayout) findViewById( R.id.refresh );
        mContainer = (ConstraintLayout) findViewById( R.id.container );
        mTabLayout = (TabLayout) findViewById( R.id.tabLayout );
        mViewPager = (ViewPager2) findViewById( R.id.viewPager );
    }

    private void initViewAction() {

        mRefresh.setOnRefreshListener( new OnRefreshListener() {
            @Override
            public void onRefresh() {

                mRefresh.postDelayed( new Runnable() {
                    @Override
                    public void run() {

                        mRefresh.setRefreshing( false );
                    }
                }, 3000 );
            }
        } );

        mViewPager.setAdapter( new Adapter( this ) );

        new TabLayoutMediator( mTabLayout, mViewPager, new TabConfigurationStrategy() {
            @Override
            public void onConfigureTab( @NonNull Tab tab, int position ) {

                tab.setText( "tab: " + position );
            }
        } ).attach();
    }

    public static class Adapter extends FragmentStateAdapter {

        public Adapter( FragmentActivity fragmentActivity ) {

            super( fragmentActivity );
        }

        @NonNull
        @Override
        public Fragment createFragment( int position ) {

            return new UiFragment();
        }

        @Override
        public int getItemCount() {

            return 2;
        }
    }

    public static class UiFragment extends Fragment {

        private RecyclerView mRecycler;

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.recycler, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );

            initView( view );
            initViewAction( view );
        }

        private void initView( View root ) {

            mRecycler = (RecyclerView) root.findViewById( R.id.recycler );

        }

        private void initViewAction( View root ) {

            mRecycler.setLayoutManager( new LinearLayoutManager( root.getContext() ) );

            List< String > data = new ArrayList<>();
            for ( int i = 0; i < 20; i++ ) {
                data.add( "item: " + i );
            }

            mRecycler.setAdapter( new SingleTypeAdapter< String >( data ) {
                @Override
                protected DataViewHolder< String > buildHolder( int position, String o ) {

                    return new DataViewHolder<>( R.layout.recycler_item_01, o ) {

                        @Override
                        protected void onBind( ViewHolder viewHolder, View root, int position, int type,
                            List< Object > payloads ) {

                        }
                    };
                }
            } );
        }
    }
}